package cipher

import (
	"crypto/hmac"
	"crypto/sha256"
	"io"
	"log"
	"os"
)

func Hmac(originData ,key []byte) []byte  {
	hash := hmac.New(sha256.New,key) // 创建对应的sha256哈希加密算法
	hash.Write(originData)
	return hash.Sum(nil)

}

func VerifyHmac(originData ,key,recvHmac []byte) bool {

	return hmac.Equal(recvHmac,Hmac(originData,key))

}
func HmacFile(filePath string ,key []byte) []byte  {

	file, err := os.Open(filePath)
	if err != nil {
		log.Print("SHA256File：打开文件错误！")
		return nil
	}
	defer file.Close()
	hash := hmac.New(sha256.New,key) // 创建对应的sha256哈希加密算法
	if _, err = io.Copy(hash, file); err != nil {
		log.Print("SHA256File：Copy错误！")
		return nil
	}
	return hash.Sum(nil)

}

func VerifyHmacFile(filePath string ,key,recvHmac []byte) bool {

	return hmac.Equal(recvHmac,HmacFile(filePath,key))

}

